// pages/scenter/addphone.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:null,
    codeinfo: null,
    allcode:0,
    verification:'textfocus-bottom-middle-but',
    token:null,
    send:true,
    alreadySend:false,
    second: 60,

  },
  //获得手机号
  getphone(e){
    let that = this
    that.setData({
      phone:e.detail.value
    })
  },
  getcode(e){
    let that = this
    that.setData({
      codeinfo:e.detail.value
    })
  },
  /*验证码点击后边颜色*/
  verification(e){
    let that = this;
    let send = that.data.send
    if(send){
    if(that.data.phone==null){
      wx.showToast({
        title: '请输入手机号',
        icon: 'error',
        duration: 2000
      })
    }else{
      let unverification='textfocus-bottom-middle-unbut';
      if(that.data.verification==unverification){
        wx.showToast({
          title: '已发送验证码 请不要重新发送',
          icon: 'error',
          duration: 2000
        })
      }
      that.setData({
        verification:unverification
      })
       //发送支付密码修改验证码短信
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
       //请求后台的分页数据
       cmd:'SendPayPwdYzm',
       mobile:that.data.phone,
       token:that.data.token
      },
      method: "get",
      success(res) {
       var data = res.data.DATA;
        if(res.data.STATUS=='Success'){
          wx.showToast({
            title: '获得验证码成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            alreadySend:true,
            send:false
          })
          that.timer()
        }else{
          wx.showToast({
            title:'请重新获取',
            icon: 'error',
            duration: 2000
          })
          that.setData({
            verification:'textfocus-bottom-middle-but'
          })
        }
      }
   })
    }
  }
  },
 //60s倒计时
 timer(e) {
  let that = this;
  let promise =new Promise((resolve, reject) => {
    let setTimer = setInterval(
      () => {
        that.setData({
          second:that.data.second - 1
        })
        if (that.data.second <= 0) {
          that.setData({
            second: 60,
            alreadySend:false,
            send:true
          })
          resolve(setTimer)
        }
      }
      , 1000)
  })
  promise.then((setTimer) => {
    clearInterval(setTimer)
  })
},

  //手机号发生绑定支付密码验证
  okbuyphone(e){
    let that = this
    let list = that.data
      if(that.data.phone==null){
        wx.showToast({
          title: '请输入手机号',
          icon: 'error',
          duration: 2000
        })
      }
      if(that.data.codeinfo==null){
        wx.showToast({
          title:'请输入验证码',
          icon: 'error',
          duration: 2000
        })
      }
    // that.setData({
    //   verification:'textfocus-bottom-middle-but'
    // })
    //跳转到输入新密码页面
    wx.navigateTo({
      url: '../scenter/editbaypass?codeinfo='+list.codeinfo+'&phone='+list.phone
    })
    // wx.request({
    //   url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
    //   data: {
    //    //请求后台的分页数据
    //    cmd:'setPayPwd',
    //    mobile:that.data.phone,
    //    token:that.data.token,
    //    yzm:that.data.code

    //   },
    //   method: "get",
    //   success(res) {
    //     if(res.data.STATUS=='Success'){
    //       wx.showToast({
    //         title: '手机验证成功',
    //         icon: 'success',
    //         duration: 2000
    //       })
    //     }else{
    //       wx.showToast({
    //         title: '手机验证失败',
    //         icon: 'error',
    //         duration: 2000
    //       })
    //     }
    //   }
    // })
  },
 //返回上一页
 navigateBack() {
  wx.navigateBack({
    delta: 1
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let token = wx.getStorageSync('token')
    that.setData({
      verification:'textfocus-bottom-middle-but',
      token:token
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})